package com.klhappy.houyuan.activity;

import java.util.HashMap;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

import com.android.volley.NetworkResponse;
import com.android.volley.Request.Method;
import com.android.volley.Response;
import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;
import com.android.volley.VolleyError;
import com.google.gson.Gson;
import com.klhappy.houyuan.R;
import com.klhappy.houyuan.bean.LoginResponseBean;
import com.klhappy.houyuan.constant.KeyConstant;
import com.klhappy.houyuan.constant.UrlConstant;
import com.klhappy.houyuan.utils.GsonRequest;
import com.klhappy.houyuan.utils.LogHelper;
import com.klhappy.houyuan.utils.headshot.CacheInfo;
import com.klhappy.houyuan.view.AbsActivity;

//登录界面
public class LoginActivity extends AbsActivity {

	private GsonRequest<LoginResponseBean> mRequest = null;
	private String mCookie = null;
	private CacheInfo cacheInfo;

	@InjectView(R.id.edit_zhanghao)
	EditText et_account;
	@InjectView(R.id.edit_mima)
	EditText et_password;
	@InjectView(R.id.tv_regist)
	TextView tvRegist;
	@OnClick(R.id.tv_regist)
	public void onClickRegist() {
		startActivity(new Intent(this, RegisterActivity.class));
	}
	@OnClick(R.id.finsh_img)
	public void finsh_img() {
		finish();
	}
	
	@InjectView(R.id.text_wangji)
	TextView tvForgetPassword;
	@OnClick(R.id.text_wangji)
	public void onClickForgetPassword() {
		startActivity(new Intent(this, GetPasswordActivity.class));
	}

	@OnClick(R.id.button_denglu)
	public void login(Button btn_login) {
		String account = et_account.getText().toString();
		String password = et_password.getText().toString();
		if (!TextUtils.isEmpty(account) && !TextUtils.isEmpty(password)) {
			// login("18108117575", "111111");
			login(account, password);
		} else {
			showToast("请正确输入");
		}
	}

	private void login(String account, String password) {
		showDialog(getResources().getString(R.string.waiting));

		mConfigManage.remove(KeyConstant.account);
		mConfigManage.remove(KeyConstant.password);
		mConfigManage.remove(KeyConstant.session);
		mConfigManage.remove(KeyConstant.myname);
		mConfigManage.remove(KeyConstant.headerimg);
		mConfigManage.remove(KeyConstant.default_bg);
		mConfigManage.remove(KeyConstant.inv_code);
		mConfigManage.remove(KeyConstant.accname);
		mConfigManage.remove(KeyConstant.Account);

		HashMap<String, String> map = new HashMap<String, String>();
		map.put("own_mob_no", account);
		map.put("own_pwd", password);
		map.put("plat_type", "1");
		Listener<LoginResponseBean> listener = new Listener<LoginResponseBean>() {
			@Override
			public void onResponse(LoginResponseBean response) {
				Gson gson = new Gson();
				Log.e("this" ,gson.toJson(response));
				if (response.code == 200) {
					mConfigManage.set(KeyConstant.account, et_account.getText().toString());
					mConfigManage.set(KeyConstant.password, et_password.getText().toString());
					if (!TextUtils.isEmpty(mCookie)) {
						mConfigManage.set(KeyConstant.session, mCookie);
					}
					mConfigManage.set(KeyConstant.myname, response.entity.getOwn_nickname());
					mConfigManage.set(KeyConstant.headerimg, response.entity.getOwn_icon());
					mConfigManage.set(KeyConstant.default_bg, response.entity.getDefault_bg());
					mConfigManage.set(KeyConstant.inv_code, response.entity.getInv_code());
					mConfigManage.set(KeyConstant.accname, response.entity.getAccname());
					mConfigManage.set(KeyConstant.Account, response.entity.getAccount());
					cacheInfo = new CacheInfo();
					cacheInfo.setPhone(et_account.getText().toString());
//					startActivity(new Intent(LoginActivity.this, Houyuan_MainActivity.class));
					LoginActivity.this.finish();
				} else {
					showToast(response.msg);
				}
				closeDialog();
			}
		};
		ErrorListener errorListener = new ErrorListener() {
			@Override
			public void onErrorResponse(VolleyError error) {
				LogHelper.e(error.toString());
				showToast(error.toString());
				closeDialog();
			}
		};
		mRequest = new GsonRequest<LoginResponseBean>(Method.POST, UrlConstant.LOGIN_URL, map, LoginResponseBean.class,
				listener, errorListener) {
			@Override
			protected Response<LoginResponseBean> parseNetworkResponse(NetworkResponse response) {
				mCookie = getCookie(response);
				LogHelper.i(mCookie);
				return super.parseNetworkResponse(response);
			}
		};
		getAppEnv().getVolleyRequest().add(mRequest);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.logon);
		ButterKnife.inject(this);
		init();
	}

	private void init() {
		String account = mConfigManage.get(KeyConstant.account, "");
		String password = mConfigManage.get(KeyConstant.password, "");

		if (!TextUtils.isEmpty(account) && !TextUtils.isEmpty(password)) {
			et_account.setText(account);
			et_password.setText(password);
		}
	}

	@Override
	public void onBackPressed() {
		super.onBackPressed();
		if (mRequest != null) {
			mRequest.cancel();
		}
	}
}
